﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.BindingHelpers;
using HaackMvcHelpers;
using Rarous.Gryphoon.Validators;
using Rarous.Gryphoon.Data;

namespace Rarous.Gryphoon.Web.Controllers {

  public class ArticleController : Controller, Rarous.Gryphoon.Web.Controllers.IArticleController {
    private const string Editor = "Editor";

    private Article article;
    private ArticleValidator validator;
    private IRepository<Article> repository;

    /// <summary>
    /// Konstruktor. Vytvoří novou instanci ArticleController.
    /// </summary>
    public ArticleController() {
      this.validator = new ArticleValidator();
    }

    public Article Article {
      get {
        return article;
      }
      set {
        article = value;
      }
    }

    public IRepository<Article> Repository {
      get {
        return repository;
      }
      set {
        repository = value;
      }
    }

    [ControllerAction]
    public void Index() {
      RenderView(Editor);
    }

    [ControllerAction]
    public void Create() {
      Article = new Article();
      Article.UpdateFrom(Request.Form);

      if (validator.Validate(Article)) {
        Repository.Save(Article);
        TempData["message"] = "Článek byl uložen.";

        RedirectToAction(new {
          Action = "Edit",
          id = Article.Id
        });
      }
    }

    [ControllerAction]
    public void Edit(int id) {
      Article = new Article();

      RenderView(Editor, Article);
    }

    [ControllerAction]
    public void Update(int id) {
      
    }

    [ControllerAction]
    public void Show(int id) {
      Article = new Article();

      RenderView("Article", Article);
    }

    [ControllerAction]
    public void Delete(int id) {
      
    }
  }
}
